package ui.timeline;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JComponent;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicSliderUI;



/**
 * UI delegate for the RangeSlider component.  RangeSliderUI paints two thumbs,
 * one for the lower value and one for the upper value.
 */
public class OneThumbTimeLineSliderUI extends BasicSliderUI 
{
	public OneThumbTimeLineSlider timeLineSlider;
	public OneThumbTimeLineRangeTrackListener listener;
	/** Color of selected range. */
	private int thumbHeight = 20;
	private int thumbWidth = 8;

	private Color mainThumbColor;	
	private Color mainThumbStrokeColor;	
	
	/** Indicator that determines whether upper thumb is being dragged. */
	public transient boolean currentDragging;

	
	/**
	 * Constructs a RangeSliderUI for the specified slider component.
	 * @param b RangeSlider
	 */
	public OneThumbTimeLineSliderUI(OneThumbTimeLineSlider b) {
		super(b);
		this.mainThumbColor = new Color(0.66f, 0.66f, 0.66f);

		this.mainThumbStrokeColor = Color.black;
		this.timeLineSlider = b;
	}
	
	public JSlider getSlider()
	{
		return this.slider;
	}
	
	public Rectangle getTrackRect()
	{
		return this.trackRect;
	}
	
	public Rectangle getThumbRect()
	{
		return this.thumbRect;
	}
	
	public boolean drawInverted()
	{
		return super.drawInverted();
	}
	
	
	public int xPositionForValue(int value)
	{
		return super.xPositionForValue(value);
	}
	/**
	 * Installs this UI delegate on the specified component. 
	 */
	@Override
	public void installUI(JComponent c) {
		super.installUI(c);
	}

	/**
	 * Creates a listener to handle track events in the specified slider.
	 */
	@Override
	protected TrackListener createTrackListener(JSlider slider) {
		listener = new OneThumbTimeLineRangeTrackListener(this);
		return listener;
	}

	/**
	 * Creates a listener to handle change events in the specified slider.
	 */
	@Override
	protected ChangeListener createChangeListener(JSlider slider) {
		return new OneThumbTimeLineChangeListener(this);
	}
	
	/**
	 * Updates the dimensions for both thumbs. 
	 */
	@Override
	protected void calculateThumbSize() {
		thumbRect.setSize(thumbWidth + 1, thumbHeight + 1);
	}
	

	/**
	 * Returns the size of a thumb.
	 */
	@Override
	protected Dimension getThumbSize() {
		return new Dimension(thumbWidth, thumbHeight);
	}

	/**
	 * Paints the slider.  The selected thumb is always painted on top of the
	 * other thumb.


	
	
	/**
	 * Paints the thumb for the upper value using the specified graphics object.
	 */
	@Override
	public void paintThumb(Graphics g) {
		Rectangle knobBounds = thumbRect;
		// Create graphics copy.
		Graphics2D g2d = (Graphics2D) g.create();

		// Create default thumb shape.
		Shape thumbShape = createThumbShape(this.thumbWidth, this.thumbHeight);

		// Draw thumb.
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.translate(knobBounds.x, knobBounds.y);

		g2d.setColor(this.mainThumbColor);
		g2d.fill(thumbShape);

		g2d.setColor(this.mainThumbStrokeColor);
		g2d.draw(thumbShape);

		g2d.drawLine(2 , this.thumbHeight / 2 -3 , this.thumbWidth - 2, this.thumbHeight / 2 - 3 );
		g2d.drawLine(2 , this.thumbHeight / 2	, this.thumbWidth - 2, this.thumbHeight / 2 );
		g2d.drawLine(2 , this.thumbHeight / 2 +3 , this.thumbWidth - 2, this.thumbHeight / 2 + 3 );

		// Dispose graphics.
		g2d.dispose();
	}

	/**
	 * Returns a Shape representing a thumb.
	 */
	private Shape createThumbShape(int width, int height) {
		// Use circular shape.
		RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, width, height, 4, 4);
		return rect;
	}
	
	@Override
	public void calculateThumbLocation() {
		// Call superclass method for lower thumb location.
		super.calculateThumbLocation();
	
		
		// Calculate upper thumb location.  The thumb is centered over its 
		// value on the track.
		int currentPosition = xPositionForValue(timeLineSlider.getValue());
		thumbRect.x = currentPosition - (thumbRect.width / 2);
		thumbRect.y = trackRect.y;
	}
}